package bb.lanxing.activity.routebook;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.res.ResourcesCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.google.android.material.appbar.AppBarLayout;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.util.route.OnChartIndicatorUpdateListener;
import bb.lanxing.util.route.OnChartScaleListener;
import bb.lanxing.util.route.OnSlopeSelectedListener;
import bb.lanxing.util.route.RouteAltitudeChartHelper;
import bb.lanxing.adapter.RouteSlopeAdapter;
import bb.lanxing.chat.utils.IntentUtils;
import bb.lanxing.common.config.Constants;
import bb.lanxing.lib.widget.utils.Density;
import bb.lanxing.model.database.RouteBookPoint;
import bb.lanxing.model.database.RouteSlope;
import bb.lanxing.mvp.presetner.RouteBookChartPresenterImpl;
import bb.lanxing.mvp.presetner.i.IRouteChartPresenter;
import bb.lanxing.mvp.view.i.IRouteChartView;
import bb.lanxing.util.CommonUtil;
import bb.lanxing.util.RecyclerViewItemClickHelper;
import bb.lanxing.util.ui.ItemHighlightAnimator;
import java.util.ArrayList;
import java.util.List;

public class RouteBookChartActivity extends BaseActivity implements
        IRouteChartView, RecyclerViewItemClickHelper.OnItemClickListener, View.OnClickListener {
    private LineChart altitudeChartView;
    private ItemHighlightAnimator animator;
    private AppBarLayout appBarLayout;
    private RouteAltitudeChartHelper chartHelper;
    private View chartIndicatorView;
    private TextView chartInfoAltitudeView;
    private TextView chartInfoDistanceView;
    private IRouteChartPresenter chartPresenter;
    private RecyclerViewItemClickHelper itemClickHelper;
    private RecyclerView levelListView;
    private TextView resumeScaleView;
    private long routeId;
    private TextView slopeInfoView;

    @Override
    public boolean orientationPortrait() {
        return false;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_route_chart);
        long longExtra = getIntent().getLongExtra("route_id", 0L);
        this.routeId = longExtra;
        if (longExtra <= 0) {
            toast(R.string.params_error);
            finish();
            return;
        }
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayShowTitleEnabled(false);
        }
        autoFitStatusBarIconColor();
        this.appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout);
        this.slopeInfoView = (TextView) findViewById(R.id.route_level_info);
        this.resumeScaleView = (TextView) findViewById(R.id.route_chart_resume_scale);
        this.altitudeChartView = (LineChart) findViewById(R.id.route_altitude_chart_view);
        this.chartIndicatorView = findViewById(R.id.route_chart_info_indicator);
        this.chartInfoDistanceView = (TextView) findViewById(R.id.route_chart_info_distance);
        this.chartInfoAltitudeView = (TextView) findViewById(R.id.route_chart_info_altitude);
        this.levelListView = (RecyclerView) findViewById(R.id.route_level_list_view);
        this.resumeScaleView.setOnClickListener(this);
        this.slopeInfoView.setOnClickListener(this);
        this.chartPresenter = new RouteBookChartPresenterImpl(this);
        this.levelListView.setLayoutManager(new LinearLayoutManager(this));
        RecyclerViewItemClickHelper recyclerViewItemClickHelper = new RecyclerViewItemClickHelper(this, null);
        this.itemClickHelper = recyclerViewItemClickHelper;
        recyclerViewItemClickHelper.attachToRecyclerView(this.levelListView);
        Drawable drawable = ResourcesCompat.getDrawable(getResources(), R.drawable.item_recycler_view_divider, getTheme());
        if (drawable != null) {
            DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, 1);
            dividerItemDecoration.setDrawable(drawable);
            this.levelListView.addItemDecoration(dividerItemDecoration);
        }
        RouteAltitudeChartHelper routeAltitudeChartHelper = new RouteAltitudeChartHelper(this.altitudeChartView, true, 6);
        this.chartHelper = routeAltitudeChartHelper;
        routeAltitudeChartHelper.setOnChartScaleListener(new OnChartScaleListener() {
            @Override
            public void onScale(float scaleX, float scaleY) {
                if (scaleX == 1.0f) {
                    List<RouteSlope> routeSlopes = RouteBookChartActivity.this.chartHelper.getRouteSlopes();
                    TextView textView = RouteBookChartActivity.this.slopeInfoView;
                    RouteBookChartActivity routeBookChartActivity = RouteBookChartActivity.this;
                    Object[] objArr = new Object[1];
                    objArr[0] = routeSlopes == null ? 0 : routeSlopes.size();
                    textView.setText(routeBookChartActivity.getString(R.string.route_chart_slope_count, objArr));
                    if (RouteBookChartActivity.this.resumeScaleView.getVisibility() == View.VISIBLE) {
                        RouteBookChartActivity.this.resumeScaleView.setVisibility(View.INVISIBLE);
                    }
                } else if (RouteBookChartActivity.this.resumeScaleView.getVisibility() != View.VISIBLE) {
                    RouteBookChartActivity.this.resumeScaleView.setVisibility(View.VISIBLE);
                }
            }
        });
        this.chartHelper.setOnSlopeSelectedListener(new OnSlopeSelectedListener() {
            @Override
            public void onSlopeSelected(final int index, RouteSlope slope, boolean fromChart) {
                RouteBookChartActivity.this.slopeInfoView.setText(RouteBookChartActivity.this.getString(R.string.route_chart_slope_level, slope.getLevel()));
                if (fromChart) {
                    RouteBookChartActivity.this.appBarLayout.setExpanded(false);
                    RouteBookChartActivity.this.levelListView.scrollToPosition(index);
                    RouteBookChartActivity.this.levelListView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if (RouteBookChartActivity.this.animator != null) {
                                RouteBookChartActivity.this.animator.cancel();
                            }
                            View findViewByPosition = RouteBookChartActivity.this.levelListView.getLayoutManager().findViewByPosition(index);
                            if (findViewByPosition != null) {
                                RouteBookChartActivity.this.animator = new ItemHighlightAnimator(findViewByPosition, 0x4dff8513, 0xffffffff);
                                RouteBookChartActivity.this.animator.start();
                            }
                        }
                    }, 100L);
                }
            }
        });
        this.chartHelper.setOnChartIndicatorUpdateListener(new OnChartIndicatorUpdateListener() {
            final int topPadding;

            {
                this.topPadding = Density.dp2px(RouteBookChartActivity.this.getApplication(), 10.0f);
            }

            @Override
            public void onAltitudePressed(float x, float y, RouteBookPoint point, float distance) {
                ViewPortHandler viewPortHandler = RouteBookChartActivity.this.altitudeChartView.getViewPortHandler();
                RouteBookChartActivity.this.chartIndicatorView.setPadding(0, ((int) viewPortHandler.offsetTop()) + this.topPadding, 0, (int) viewPortHandler.offsetBottom());
                if (x < viewPortHandler.contentLeft()) {
                    x = viewPortHandler.contentLeft();
                } else if (x > viewPortHandler.contentRight()) {
                    x = viewPortHandler.contentRight();
                }
                RouteBookChartActivity.this.chartIndicatorView.setTranslationX((int) x);
                RouteBookChartActivity.this.chartInfoDistanceView.setText(RouteBookChartActivity.this.getString(R.string.route_chart_info_distance_placeholder, CommonUtil.getFormatDistance(distance)));
                RouteBookChartActivity.this.chartInfoAltitudeView.setText(RouteBookChartActivity.this.getString(R.string.route_chart_info_altitude_placeholder, String.valueOf((int) point.getAltitude())));
                if (RouteBookChartActivity.this.chartIndicatorView.getVisibility() != View.VISIBLE) {
                    RouteBookChartActivity.this.chartIndicatorView.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onIndicatorMove(float x) {
                ViewPortHandler viewPortHandler = RouteBookChartActivity.this.altitudeChartView.getViewPortHandler();
                if (x < viewPortHandler.contentLeft()) {
                    x = viewPortHandler.contentLeft();
                } else if (x > viewPortHandler.contentRight()) {
                    x = viewPortHandler.contentRight();
                }
                RouteBookChartActivity.this.chartIndicatorView.setTranslationX((int) x);
            }

            @Override
            public void onIndicatorCanceled() {
                if (RouteBookChartActivity.this.chartIndicatorView.getVisibility() == View.VISIBLE) {
                    RouteBookChartActivity.this.chartIndicatorView.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @Override
    public void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        if (savedInstanceState == null) {
            this.chartPresenter.loadChart(this.routeId);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putLong("route_id", this.routeId);
        if (this.chartHelper.getAltitudePoints() != null && this.chartHelper.getDistances() != null) {
            ArrayList<? extends Parcelable> arrayList = new ArrayList<>(this.chartHelper.getAltitudePoints());
            List<Float> distances = this.chartHelper.getDistances();
            float[] fArr = new float[distances.size()];
            for (int i = 0; i < distances.size(); i++) {
                Float f = distances.get(i);
                fArr[i] = f == null ? 0.0f : f;
            }
            outState.putParcelableArrayList("altitude_points", arrayList);
            outState.putFloatArray("distance_array", fArr);
        }
        if (this.chartHelper.getRouteSlopes() != null) {
            outState.putParcelableArrayList("route_slopes", new ArrayList<>(this.chartHelper.getRouteSlopes()));
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        this.routeId = savedInstanceState.getLong("route_id");
        ArrayList parcelableArrayList = savedInstanceState.getParcelableArrayList("altitude_points");
        float[] floatArray = savedInstanceState.getFloatArray("distance_array");
        if (parcelableArrayList != null && floatArray != null) {
            ArrayList arrayList = new ArrayList(floatArray.length);
            for (float f : floatArray) {
                arrayList.add(f);
            }
            this.chartHelper.initChart(parcelableArrayList, arrayList);
            ArrayList parcelableArrayList2 = savedInstanceState.getParcelableArrayList("route_slopes");
            if (parcelableArrayList2 != null) {
                onLoadSlope(parcelableArrayList2);
                return;
            } else {
                this.chartPresenter.loadSlope(this.routeId);
                return;
            }
        }
        this.chartPresenter.loadChart(this.routeId);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_route_chart, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (R.id.route_chart_close == item.getItemId()) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onLoadChart(List<RouteBookPoint> lushuPoints, List<Float> distances) {
        this.chartHelper.initChart(lushuPoints, distances);
        this.slopeInfoView.setText(R.string.route_chart_slope_loading);
        this.chartPresenter.loadSlope(this.routeId);
    }

    @Override
    public void onLoadSlope(List<RouteSlope> slopes) {
        this.slopeInfoView.setText(getString(R.string.route_chart_slope_count, slopes.size()));
        this.levelListView.setAdapter(new RouteSlopeAdapter(slopes));
        this.chartHelper.initSlope(slopes);
    }

    @Override
    public void exit() {
        finish();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.chartPresenter.destroy();
        this.itemClickHelper.destroy();
    }

    @Override
    public void onItemClick(RecyclerView.ViewHolder viewHolder) {
        this.chartHelper.zoomSlope(viewHolder.getAdapterPosition());
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.route_chart_resume_scale) {
            this.chartHelper.collapsedSlopes();
        } else if (id == R.id.route_level_info) {
            IntentUtils.gotoWebBrowser(this, Constants.ROUTE_SLOPE_LEVEL_HELP, null);
        }
    }
}
